﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.Text;

public partial class MRP_ImportExport : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Params["type"] != null)
        {
            string xmlFileName = string.Format(@"XML/MRP_{0}.xml", Request.Params["type"].ToString());
            this.UIServer1.XMLfile = xmlFileName;
        }
    }
    protected void UIServer1_DBChange(object sender, Evolutility.UIServer.DatabaseEventArgs e)
    {
        string param = "";
        int id = 0;
        Dictionary<string, string> oldValues = null;
        Dictionary<string, string> newValues = null;
        StringBuilder sbSQL = new StringBuilder();

        if (Request.Params["type"] != null)
        {
            param = Request.Params["type"].ToString();
            id=e.ID;

            Database db = DatabaseFactory.CreateDatabase("MRPSqlServer");
            DbCommand dbCommand = null;

            //如果是材料入库，需要减少在途，增加在库数量
            //入库时需要转化成库存单位的数量
            if (param.ToLower().Equals("material_import"))
            {
                decimal oldQuantity = 0,oldweight=0;
                int oldOrderID = 0;
                int oldMID = 0;
                oldValues = e.OldValues;
                if (oldValues != null)
                {
                    oldQuantity=decimal.Parse(MRPUtility.GetOldValueFromString("Quantity", oldValues, "0"));
                    oldOrderID = Int32.Parse(MRPUtility.GetOldValueFromString("OrderID", oldValues, "0"));
                    oldMID = Int32.Parse(MRPUtility.GetOldValueFromString("MaterialID", oldValues, "0"));
                    oldweight = decimal.Parse(MRPUtility.GetOldValueFromString("Weight", oldValues, "0"));

                }
                string sqlCommand = "MRP_UpdatePurchaseStatusForMaterialImport";
                dbCommand = db.GetStoredProcCommand(sqlCommand);
                db.AddInParameter(dbCommand, "ItemID", DbType.Int32, id);
                db.AddInParameter(dbCommand, "oldQuantity", DbType.Decimal, oldQuantity);
                db.AddInParameter(dbCommand, "oldOrderID", DbType.Int32, oldOrderID);
                db.AddInParameter(dbCommand, "oldMaterialID", DbType.Int32, oldMID);
                db.AddInParameter(dbCommand, "oldWeight", DbType.Decimal, oldweight);
                MRPUtility.WriteGeneralLog(string.Format("试图用存储过程{0}更新采购订单的状态及库存的状态！", sqlCommand));


            }
            else if (param.ToLower().Equals("material_export_return"))
            {
                decimal oldQuantity = 0,oldweight=0;
                int oldOrderID = 0;
                int oldMID = 0;
                oldValues = e.OldValues;
                if (oldValues != null)
                {
                    oldQuantity=decimal.Parse(MRPUtility.GetOldValueFromString("Quantity", oldValues, "0"));
                    oldOrderID = Int32.Parse(MRPUtility.GetOldValueFromString("OrderID", oldValues, "0"));
                    oldMID = Int32.Parse(MRPUtility.GetOldValueFromString("MaterialID", oldValues, "0"));
                    oldweight = decimal.Parse(MRPUtility.GetOldValueFromString("Weight", oldValues, "0"));

                }
                string sqlCommand = "MRP_UpdatePurchaseStatusForMaterialExportReturn";
                dbCommand = db.GetStoredProcCommand(sqlCommand);
                db.AddInParameter(dbCommand, "ItemID", DbType.Int32, id);
                db.AddInParameter(dbCommand, "oldQuantity", DbType.Decimal, oldQuantity);
                db.AddInParameter(dbCommand, "oldOrderID", DbType.Int32, oldOrderID);
                db.AddInParameter(dbCommand, "oldMaterialID", DbType.Int32, oldMID);
                db.AddInParameter(dbCommand, "oldWeight", DbType.Decimal, oldweight);
                MRPUtility.WriteGeneralLog(string.Format("试图用存储过程{0}更新采购订单的状态及库存的状态！", sqlCommand));
            }

            else if (param.ToLower().Equals("product_import"))
            {
                decimal oldQuantity = 0;

                oldValues = e.OldValues;
                if (oldValues != null)
                {
                    oldQuantity = Int32.Parse(MRPUtility.GetOldValueFromString("Quantity", oldValues, "0"));

                }
                string sqlCommand = "MRP_UpdateStockForProductImport";
                dbCommand = db.GetStoredProcCommand(sqlCommand);
                db.AddInParameter(dbCommand, "ItemID", DbType.Int32, id);
                db.AddInParameter(dbCommand, "oldQuantity", DbType.Decimal, oldQuantity);
                MRPUtility.WriteGeneralLog(string.Format("试图用存储过程{0}更新库存的状态！", sqlCommand));

                //update MRP_Stock set InStore=InStore+@q,InTransit=InTransit-@q where MaterialID=@mID;

            }
            else if (param.ToLower().Equals("material_import_inner"))
            {
                decimal oldQuantity = 0;

                int oldMID = 0;
                oldValues = e.OldValues;
                if (oldValues != null)
                {
                    oldQuantity = decimal.Parse(MRPUtility.GetOldValueFromString("QuantityOfStockUnit", oldValues, "0"));
                    oldMID = Int32.Parse(MRPUtility.GetOldValueFromString("MaterialID", oldValues, "0"));


                }
                string sqlCommand = "MRP_UpdatePurchaseStatusForMaterialImportInner";
                dbCommand = db.GetStoredProcCommand(sqlCommand);
                db.AddInParameter(dbCommand, "ItemID", DbType.Int32, id);
                db.AddInParameter(dbCommand, "oldQuantity", DbType.Decimal, oldQuantity);
                db.AddInParameter(dbCommand, "oldMaterialID", DbType.Int32, oldMID);

                MRPUtility.WriteGeneralLog(string.Format("试图用存储过程{0}更新库存的状态！", sqlCommand));
            }
            else if (param.ToLower().Equals("material_export"))
            {
                decimal oldQuantity = 0;

                int oldMID = 0;
                oldValues = e.OldValues;
                if (oldValues != null)
                {
                    oldQuantity = decimal.Parse(MRPUtility.GetOldValueFromString("QuantityOfStockUnit", oldValues, "0"));
                    oldMID = Int32.Parse(MRPUtility.GetOldValueFromString("MaterialID", oldValues, "0"));


                }
                string sqlCommand = "MRP_UpdatePurchaseStatusForMaterialExport";
                dbCommand = db.GetStoredProcCommand(sqlCommand);
                db.AddInParameter(dbCommand, "ItemID", DbType.Int32, id);
                db.AddInParameter(dbCommand, "oldQuantity", DbType.Decimal, oldQuantity);
                db.AddInParameter(dbCommand, "oldMaterialID", DbType.Int32, oldMID);

                MRPUtility.WriteGeneralLog(string.Format("试图用存储过程{0}更新库存的状态！", sqlCommand));

            }
            else if (param.ToLower().Equals("product_export_repair") || param.ToLower().Equals("product_export") || param.ToLower().Equals("product_export_delivery_sign"))
            {

                //if (e.Action == Evolutility.UIServer.DBAction.INSERT)
                {
                    id = e.ID;
                    decimal oldQuantity = 0;
                    decimal oldSignQuantity = 0;
                    oldValues = e.OldValues;
                    if (oldValues != null)
                    {
                        oldQuantity = decimal.Parse(MRPUtility.GetOldValueFromString("Quantity", oldValues, "0"));
                        oldSignQuantity = decimal.Parse(MRPUtility.GetOldValueFromString("QuantityOfSign", oldValues, "0"));




                    }
                    string sqlCommand = "MRP_CheckOrderStatus";
                    dbCommand = db.GetStoredProcCommand(sqlCommand);
                    db.AddInParameter(dbCommand, "ItemID", DbType.Int32, id);
                    db.AddInParameter(dbCommand, "oldQuantity", DbType.Decimal, oldQuantity);
                    db.AddInParameter(dbCommand, "oldSignQuantity", DbType.Decimal, oldSignQuantity);

                    MRPUtility.WriteGeneralLog("试图更新客户订单的状态！");

                }
            }
            if(dbCommand!=null)
                db.ExecuteNonQuery(dbCommand);
        }
    }
}
